<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class ReviewController extends AdminbaseController{
    
	protected $review_model;
	protected $classify_model;

	public function _initialize() {
		parent::_initialize();
		$this->review_model = D("Common/Review");
		$this->classify_model = D("Common/Classify");
	}
	
	// 后台评论列表
	public function index(){
       $sql = " SELECT cr.*,cou.`avatar` head_img,cou.`user_nicename` username,cc.`name` cookname FROM cmf_review cr
                LEFT JOIN cmf_users cou ON cou.id = cr.uid
                LEFT JOIN cmf_cookbook cc ON cc.id = cr.cook_id
                LEFT JOIN cmf_review_image cri ON cri.review_id = cr.id
                 WHERE cr.review_id = 0
                ORDER BY cr.create_time DESC";
		$reviews =$this->review_model->query($sql);
		$this->assign("reviews",$reviews);
		$this->display();
	}
	
	// 管理员回复评论添加
	public function addReply(){
        $id = I("get.id",0,"intval");
        $cookId = I("get.cook_id");
        $this->assign("id",$id);
        $this->assign("cookId",$cookId);
		$this->display();
	}
	
	// 管理员回复评论添加提交
	public function addReply_post(){
        $_POST['create_time'] = date("Y-m-d H:i:s", time());
        $_POST['user_id'] = 0;
        $_POST['uid'] = get_current_admin_id();
		if(IS_POST){
			if ($this->review_model->create()!==false){
                $id = $this->review_model->add();
				if ($id!==false) {
					$this->success(L('ADD_SUCCESS'), U("Review/index"));
				} else {
					$this->error(L('ADD_FAILED'));
				}
			} else {
				$this->error($this->review_model->getError());
			}
		
		}
	}
    // 查看所有回复
    public function viewReply(){
        $id = I("get.id",0,"intval");
        $sql = " SELECT cr.*,cou.`avatar` head_img,cou.`user_nicename` username FROM cmf_review cr
                LEFT JOIN cmf_users cou ON cou.id = cr.uid
                 WHERE cr.review_id = $id
                ORDER BY cr.create_time DESC";
        $reviews =$this->review_model->query($sql);
        $this->assign("reviews",$reviews);
        $this->display();
    }

	//删除评论
	public function delete(){
		$id = I("get.id",0,"intval");
		if ($this->review_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}


	
}